package edu.hqu.rent_system.controller;

import com.alibaba.fastjson.JSON;
import edu.hqu.rent_system.common.Result;
import edu.hqu.rent_system.pojo.*;
import edu.hqu.rent_system.service.HouseService;
import edu.hqu.rent_system.service.LandLordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class LandLordController {
    @Autowired
    private LandLordService landLordService;

    @RequestMapping("/LandLord/addHouse")
    public Result addHouse(@RequestBody String houseStr) {
        try {
            House house = JSON.parseObject(houseStr, House.class);
            int record = landLordService.addHouse(house);//获取自增的主键值
            return Result.success(house.getHId());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/updateHouse")
    public Result updateHouse(@RequestBody(required = false)  String houseStr) {
        try {
            House house = JSON.parseObject(houseStr, House.class);
            int i = landLordService.updateHouse(house);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/selectHouse")
    public Result selectHouse(@RequestParam("lId") int lId) {
        try {
            List<House> housesList= landLordService.selectHouse(lId);
            if (housesList != null || housesList.size() != 0){
                return Result.success(housesList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/deleteHouse")
    public Result deleteHouse(@RequestParam("hId") int hId) {
        try {
            int i = landLordService.deleteHouse(hId);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/selectMeeting")
    public Result selectMeeting(@RequestParam("lId") int lId) {
        try {
            List<Meeting> meetingList = landLordService.selectMeeting(lId);
            if (meetingList != null || meetingList.size() != 0){
                return Result.success(meetingList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/selectTenantOfMine")
    public Result selectTenantOfMine(@RequestParam("lId") int lId) {
        try {
            List<Tenant> tenantList = landLordService.selectTenantOfMine(lId);
            if (tenantList != null || tenantList.size() != 0){
                return Result.success(tenantList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/selectConfirmedMeeting")
    public Result selectConfirmedMeeting(@RequestParam("lId") int lId) {
        try {
            List<Meeting> meetingList = landLordService.selectConfirmedMeeting(lId);
            if (meetingList != null || meetingList.size() != 0){
                return Result.success(meetingList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/selectRejectedMeeting")
    public Result selecRejectedtMeeting(@RequestParam("lId") int lId) {
        try {
            List<Meeting> meetingList = landLordService.selectRejectedMeeting(lId);
            if (meetingList != null || meetingList.size() != 0){
                return Result.success(meetingList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/confirmMeeting")
    public Result confirmMeeting(@RequestParam("mId") int mId) {
        try {
            int i=landLordService.confirmMeeting(mId);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/rejectMeeting")
    public Result rejectMeeting(@RequestParam("mId") int mId) {
        try {
            int i = landLordService.rejectMeeting(mId);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/selectRecord")
    public Result selectRecord(@RequestParam("lId") int lId) {
        try {
            List<Record> recordList = landLordService.selectRecord(lId);
            if (recordList != null || recordList.size() != 0){
                return Result.success(recordList);
            }
            return Result.success(recordList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/selectPay")
    public Result selectPay(@RequestParam int lId) {
        try {
            List<Pay> payList = landLordService.selectPay(lId);
            if (payList != null || payList.size() != 0) {
                return Result.success(payList);
            }
            return Result.success(payList);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/getLandLordInfo")
    public Result getLandLordInfo(@RequestParam int lId) {
        try {
            LandLord landLord = landLordService.getLandLordInfo(lId);
            return Result.success(landLord);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }


    @RequestMapping("/LandLord/updateLandLordInfo")
    public Result updateLandLordInfo(@RequestBody String landLordStr) {
        try {
            LandLord landLord = JSON.parseObject(landLordStr, LandLord.class);
            int i = landLordService.updateLandLordInfo(landLord);
            return Result.success(i);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/LandLord/insertRecord")
    public Result insertRecord(@RequestBody String recordStr) {
        try {
            Record record = JSON.parseObject(recordStr, Record.class);
            House house = landLordService.selectHouseByhId(record.getHId());
            record.setRentPrice(house.getRentPrice());
            Calendar calendar= Calendar.getInstance();
            Date date=calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            record.setStartTime(sdf.format(date));

            calendar.add(Calendar.MONTH,1);
            date=calendar.getTime();
            record.setEndTime(sdf.format(date));
            System.out.println(record);

            int i = landLordService.insertRecord(record);
            return Result.success(i);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    @RequestMapping("/LandLord/insertPay")
    public Result insertPay(@RequestBody String payStr) {
        try {
            System.out.println(payStr + "222122");
            Pay pay = JSON.parseObject(payStr, Pay.class);
            Calendar calendar= Calendar.getInstance();
            Date date=calendar.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            pay.setPTime(sdf.format(date));
            System.out.println(pay + "11111111");
            int i = landLordService.insertPay(pay);
            return Result.success(i);
        }catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
}
